//
// Copyright 2007 Brian Watkins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

var noteWrapper;

var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ];

function getMonth ( m )
{
  return months[m];
}

function handleNoteEvent ()
{
  //nothing. pages can override to implement functionality
}

function getNotes ()
{
  var surl = 'note?action=getNotes&id=' + noteManagerId;

  var callback = 
  {
    success : parseNoteDoc,
    failure : handleFailure
  };

  YAHOO.util.Connect.asyncRequest( 'GET', surl, callback );
}

function parseNoteDoc ( obj )
{
  var doc = obj.responseXML;
  var notes = doc.getElementsByTagName( "note" );

  //notes are ordered by the server from oldest to newest.
  //we display them with the newest at the top
  for ( var i = ( notes.length - 1 ); i > -1; i-- ) {
    var id = notes.item( i ).getAttribute( "id" );
    var year = notes.item( i ).getAttribute( "year" );
    var month = notes.item( i ).getAttribute( "month" );
    var date = notes.item( i ).getAttribute( "date" );
    var time = notes.item( i ).getAttribute( "time" );
    var data = notes.item( i ).childNodes.item( 0 ).nodeValue;

    var note = makeNote( id, formatDate( year, month, date, time ), data );

    document.getElementById( "notes" ).appendChild( note );
  }

  handleNoteEvent();
}

function makeNote ( id, time, data )
{
  var note = document.createElement( "div" );
  note.setAttribute( "class", "note" );
  note.setAttribute( "id", id );
  note.setAttribute( "mytime", time );

  var title = document.createElement( "div" );
  title.setAttribute( "class", "noteTitle" );

  var ttab = document.createElement( "TABLE" );
  ttab.setAttribute( "cellpadding", "0" );
  ttab.setAttribute( "cellspacing", "0" );
  ttab.setAttribute( "border", "0" );
  ttab.setAttribute( "width", "100%" );
  var ttr = document.createElement( "TR" );
  
  var dt = document.createElement( "TD" );
  dt.setAttribute( "class", "noteDate" );
  dt.appendChild( document.createTextNode( time ) );

  ttr.appendChild( dt );

  var act = document.createElement( "TD" );
  act.setAttribute( "class", "noteActions" );
  act.innerHTML = "<a class=\"noteActionLink\" href=\"javascript:editNote( '" + id + "' )\">edit</a> | <a class=\"noteActionLink\" href=\"javascript:deleteNote( '" + id + "' )\">delete</a>";

  ttr.appendChild( act );

  ttab.appendChild( ttr );
  title.appendChild( ttab );

  note.appendChild( title );

  //var l = document.createElement( "div" );
  //l.setAttribute( "class", "line" );
  //note.appendChild( l );

  var d = document.createElement( "div" );
  d.setAttribute( "class", "noteData" );
  d.setAttribute( "id", "noteText" );
  //need to wrap in p-tag for styling to work on firefox for some reason. 
  d.innerHTML = "<p>" + data + "</p>";

  note.appendChild( d );

  return note;
}

function addNote ()
{
  var newNote = makeUpdateNote( '', '', '', '', '', '' );
  var notesDiv = document.getElementById( "notes" );
  notesDiv.insertBefore( newNote, notesDiv.firstChild );

  //document.forms.noteForm.data.focus();
  var editor = new TinyEditor( "noteDataTA", 'include/tinyEditor_css.jsp' );
  openEditor( editor );
}

function openEditor ( editor )
{
  editor.init( true );
  editor.focus();
  editor.unlock();
}

function addNoteForDate ( year, month, date )
{
  var newNote = makeUpdateNote( '', '', formatDate( year, month, date, '' ), year, month, date );
  var notesDiv = document.getElementById( "notes" );
  notesDiv.insertBefore( newNote, notesDiv.firstChild );

  document.forms.noteForm.data.focus();
}

function editNote ( id )
{
  var currentNote = findNote( id );

  var data = '';
  var divs = currentNote.getElementsByTagName( "div" );
  for ( var i = 0; i < divs.length; i++ ) {
    if ( divs.item( i ).getAttribute( "id" ) == 'noteText' ) {
      data = divs.item( i ).innerHTML;
      break;
    }
  }

  var time = currentNote.getAttribute( "mytime" );

  var note = makeUpdateNote( id, data, time, '', '', '' );

  var nDiv = document.getElementById( "notes" );
  nDiv.replaceChild( note, currentNote );

  var editor = new TinyEditor( "noteDataTA", "include/tinyEditor_css.jsp" );
  openEditor( editor );

  //document.forms.noteForm.data.focus();
}

function findNote ( id )
{
  var note;
  var nDivChildren = document.getElementById( "notes" ).getElementsByTagName( "DIV" );
  for ( var i = 0; i < nDivChildren.length; i++ ) {
    if ( nDivChildren.item( i ).getAttribute( "id" ) == id ) {
      note = nDivChildren.item( i );
      break;
    }
  }
  return note;
}

function cancelNote ( id )
{
  //get rid of the editor
  TinyEditor.getEditor( "noteDataTA" ).destroy();
  
  var notes = document.getElementById( "notes" );
  var nDivChildren = notes.childNodes;
  for ( var i = 0; i < nDivChildren.length; i++ ) {
    if ( nDivChildren.item( i ).getAttribute( "id" ) == 'updateNote' ) {
      notes.removeChild( nDivChildren.item( i ) );
      break;
    }
  }

  if ( typeof( afterSavedNote ) != 'undefined' ) {
    afterSavedNote();
  }

  handleNoteEvent();
}

function makeUpdateNote ( id, data, time, year, month, date )
{
  var note = document.createElement( "div" );
  note.setAttribute( "id", "updateNote" );
  note.setAttribute( "class", "note" );

  var title = document.createElement( "div" );
  title.setAttribute( "class", "noteTitle" );

  var ttab = document.createElement( "TABLE" );
  ttab.setAttribute( "cellpadding", "0" );
  ttab.setAttribute( "cellspacing", "0" );
  ttab.setAttribute( "border", "0" );
  ttab.setAttribute( "width", "100%" );

  var ttr = document.createElement( "TR" );

  var dt = document.createElement( "TD" );
  dt.setAttribute( "class", "noteDate" );
  if ( time != '' ) {
    dt.innerHTML = time;
  } else {
    dt.innerHTML = "&nbsp;";
  }

  ttr.appendChild( dt );

  var act = document.createElement( "TD" );
  act.setAttribute( "class", "noteActions" );
  act.innerHTML = "<a class=\"noteActionLink\" href=\"javascript:saveNote( '" + id + "' )\">save</a>";
  if ( id == '' ) {
    act.innerHTML += " | <a class=\"noteActionLink\" href=\"javascript:cancelNote()\">cancel</a>";
  }

  ttr.appendChild( act );

  ttab.appendChild( ttr );
  title.appendChild( ttab );

  note.appendChild( title );

  //var l = document.createElement( "div" );
  //l.setAttribute( "class", "line" );
  //note.appendChild( l );

  var d = document.createElement( "div" );
  d.setAttribute( "class", "noteData" );

  var f = document.createElement( "form" );
  f.setAttribute( "id", "noteForm" );
  var ta = document.createElement( "textarea" );
  ta.setAttribute( "id", "noteDataTA" );
  ta.setAttribute( "class", "noteEditor" );
  ta.setAttribute( "name", "data" );
  //ta.setAttribute( "rows", "4" );
  //ta.setAttribute( "cols", "39" );
  //ta.setAttribute( "style", "width:100%;" );
  ta.appendChild( document.createTextNode( data ) );
  f.appendChild( ta );

  var yh = document.createElement( "input" );
  yh.setAttribute( "type", "hidden" );
  yh.setAttribute( "name", "year" );
  yh.setAttribute( "value", year );
  f.appendChild( yh );

  var mh = document.createElement( "input" );
  mh.setAttribute( "type", "hidden" );
  mh.setAttribute( "name", "month" );
  mh.setAttribute( "value", month );
  f.appendChild( mh );

  var dh = document.createElement( "input" );
  dh.setAttribute( "type", "hidden" );
  dh.setAttribute( "name", "date" );
  dh.setAttribute( "value", date );
  f.appendChild( dh );

  d.appendChild( f );

  note.appendChild( d );

  return note;
}

function saveNote ( id )
{
  //var data = document.forms.noteForm.data.value;
  var data = TinyEditor.getEditor( "noteDataTA" ).getValue();
  var year = document.forms.noteForm.year.value;
  var month = document.forms.noteForm.month.value;
  var date = document.forms.noteForm.date.value;

  if ( !data ) { data = ' '; }

  var surl = 'note?action=save&id=' + noteManagerId + 
             '&data=' + escape( data ) + '&noteId=' + id + 
	     '&year=' + year + '&month=' + month + '&date=' + date;

  var callback = 
  {
    success : handleSaveNote,
    failure : handleFailure
  };

  YAHOO.util.Connect.asyncRequest( 'GET', surl, callback );
}

function handleSaveNote ( obj )
{
  var doc = obj.responseXML;
  
  //get rid of the editor
  TinyEditor.getEditor( "noteDataTA" ).destroy(); 

  //remove all child nodes and replace with the
  //new list of notes. it could be reordered by date on the server side
  var ndivs = document.getElementById( "notes" );
  var ndivChildren = ndivs.childNodes;
  var length = ndivChildren.length;
  for ( var i = 0; i < length; i++ ) {
    ndivs.removeChild( ndivChildren.item( 0 ) );
  }

  parseNoteDoc( obj );

  if ( typeof( afterSavedNote ) != 'undefined' ) {
    afterSavedNote();
  }
}

function deleteNote ( id )
{
  var surl = 'note?action=delete&id=' + noteManagerId + '&noteId=' + escape( id );

  var callback = 
  {
    success : handleDelete,
    failure : handleFailure
  };

  YAHOO.util.Connect.asyncRequest( 'GET', surl, callback );
}

function handleDelete ( obj )
{
  var doc = obj.responseXML;

  var notes = doc.getElementsByTagName( "note" );
  var id = notes.item( 0 ).getAttribute( "id" );

  var ndivs = document.getElementById( "notes" );
  var ndivChildren = ndivs.childNodes;

  for ( var i = 0; i < ndivChildren.length; i++ ) {
    var node = ndivChildren.item( i );
    if ( node.nodeType == 1 && node.getAttribute( "id" ) == id ) {
      ndivs.removeChild( ndivChildren.item( i ) );
      break;
    }
  }

  handleNoteEvent();
}

